import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router/index'
// create an axios instance
const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 5000 // request timeout
})
// 请求拦截器，在请求发送前做一些操作
service.interceptors.request.use(config => {
  return config
})
service.interceptors.response.use(
  response => {
    console.log(1)
    console.log(response)
    const res = response.data
    if (res.code === 1) {
      Promise.resolve(response)
      router.push('/login')
    } else {
      return response
    }
  },
  error => {
    console.log('err' + error)// for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
